package net.udp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class SenderThread extends Thread
{
	private InetAddress server;
	private DatagramSocket socket;
	private boolean stopped = false;
	private int port;
	public SenderThread(InetAddress address,int port) throws SocketException
	{
		this.server = address;
		this.port = port;
		this.socket = new DatagramSocket();
		this.socket.connect(server,port);
	}
	public void halt()
	{
		this.stopped = true;
	}
	public DatagramSocket getSocket()
	{
		return this.socket;
	}
	@Override
	public void run()
	{
		try
		{
			BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
			while(true)
			{
				if (stopped) return;
				String theLine = userInput.readLine();
				if(theLine.equals("."))
				{
					break;
				}
				byte[] data = theLine.getBytes();
				DatagramPacket output = new DatagramPacket(data, data.length,server,port);
				socket.send(output);
				Thread.yield();
			}
		}
		catch (Exception e)
		{
		}
	}
}
